<template>
  <div>
    <!-- 1.导航栏 -->
    <van-nav-bar
      :title="` ${$route.query.q}的搜索结果`"
      left-arrow
      @click-left="$router.back()"
      fixed
      placeholder
    />
    <!-- 2.搜索结果列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell 
      v-for="item in list" 
      :key="item.art_id" 
      :title="item.title" 
      @click="$router.push({path:'/article',query:{id:item.art_id}})"
      />
    </van-list>
  </div>
</template>

<script>
//导入接口
import { getSearchList } from './home.js'

export default {
  name: 'searchResult',
  data () {
    return {
      loading: false, //加载更多动画
      finished: false, //是否全部加载完毕
      list: [], //搜索结果
      page: 1 //当前页数
    }
  },
  methods: {
    //加载更多
    async onLoad () {
      //1.加载数据
      let res = await getSearchList({
          page:this.page,
          per_page:10,
          q:this.$route.query.q
      })
      console.log(res)
      //绑定数据
      this.list.push(...res.data.data.results)
      //页码自增
      this.page++
      //2.关闭加载更多动画
      this.loading = false

      //3.如果服务器返回空数组，表示全部加载完毕
      if (res.data.data.results.length == 0) {
        this.finished = true
      }
    }
  }
}
</script>

<style></style>
